/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.List;
import models.Student;
import models.Role;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author Achie
 */
public class RoleDao {

    private final Session session;

    public RoleDao(Session session) {
        this.session = session;
    }

    public List<Role> findAll() {
        Transaction tx = session.beginTransaction();
        List<Role> list = session.createQuery("from Role as r ORDER BY r.name ASC").list();
        tx.commit();
        return list;
    }

    public boolean save(Role role) {
        try {
            Transaction tx = session.beginTransaction();
            session.save(role);
            tx.commit();
            return true;
        } catch (Exception e) {
           // System.out.println("Error during save: " + e);
            return false;
        }
    }

    public boolean update(Role role) {
        try {
            Transaction tx = session.beginTransaction();
            session.update(role);
            tx.commit();
            return true;
        } catch (Exception e) {
           // System.out.println("Error during update: " + e);
            return false;
        }
    }

    public Role get(long id) {
        return (Role) session.get(Role.class, id);
    }

    public void delete(Role role) {
        Transaction tx = session.beginTransaction();
        session.delete(role);
        tx.commit();
    }
}
